package com.jaemon.commons.toolkit;

import com.jaemon.commons.toolkit.exception.DeepCopyException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * BeanUtils
 *
 * @author Jaemon
 */
public class BeanUtils {

    /**
     * list deepCopy
     *
     * @param src list source
     * @param <T> type
     * @return result
     */
    public static <T> List<T> deepCopy(List<T> src) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(src);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            List<T> dest = (List<T>) in.readObject();
            return dest;
        } catch (Exception e) {
            throw new DeepCopyException(e);
        }
    }
}